import { defineStore } from "pinia";
import * as api from '../api/index'
import recursionRoutes from '../utills/recursionRoutes'
//引入动态路由
import dynamicRoutes from "../router/dynamicRoutes";
//引入静态路由
import staticRoute from "../router/staticRoute";
//引入路由对象
import router from '../router/index'
import routes from '../utills/autoRouter'

export let usePermission = defineStore('permission', {
    state() {
        return {
            menulsit: [],
        }
    },
    actions: {
        fetch_menulist() {
            //发送请求获取菜单
            return api.getPermission()
                .then(res => {
                    // console.log(res);
                    // let routess = routes
                    let r = recursionRoutes(res.data.menuList,routes)
                    this.menulsit = r
                    // console.log(r);
                    //动态添加路由
                    //1.先从动态路由中找到路径是"/"的页面
                    let target = dynamicRoutes.find(item => item.path === "/")
                    //2.将用户路由变成它的子路由
                    target.children = r
                    // console.log(r);
                    //3.将静态路由也添加到动态路由中
                    target.children = [...target.children, ...staticRoute]
                    //4.通过addRoute方法，将动态路由添加到routes配置中
                    router.addRoute(target)
                })
        }
    }
})